package com.bigchun.woodhunter;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

import bean.ContentDataBean;
import myapp.MyApp;
import util.MyLog;
import util.RequestManager;
import util.SystemStatusManager;

public class WebviewTreeActivity extends BaseActivity {


    private LinearLayout ll_reback;
    private MyApp myapp;
    private Context context;
    private String token;
    private String z_about;
    private String e_about;
    private String domain;
    private WebView webview;
    private String yuyan = "";
    private String apptype = "";
    private TextView tv_title;
    private TextView tv_titlename;
    private TextView tv_time;
    private String goodsid;
    private String title = "";
    private String intro = "";
    private String content = "";
    private String createtime = "";
    private String textData;
    private ContentDataBean.DataBean textContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_webviewtree);
        context = this;
        myapp = (MyApp) getApplicationContext();
        SharedPreferences sharedPreferences = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        token = sharedPreferences.getString("token", "");
        apptype = sharedPreferences.getString("apptype", "0");
        goodsid = getIntent().getStringExtra("goodsid");
        textData = sharedPreferences.getString("textData", "");
        setTranslucentStatus();
        initView();
        initLin();
        getData();

    }
    private void initView() {
        ll_reback = findViewById(R.id.ll_reback);
        tv_title = findViewById(R.id.tv_title);
        tv_titlename = findViewById(R.id.tv_titlename);
        tv_time = findViewById(R.id.tv_time);
        webview = findViewById(R.id.webview);
        textContent = new Gson().fromJson(textData, new TypeToken<ContentDataBean.DataBean>() {}.getType());
        if (textContent != null){
            tv_titlename.setText(textContent.getTreetype_info());
        }



    }

    private void initLin() {
        ll_reback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    /**
     * 请求数据
     */
    private void getData() {
        String url = myapp.getWebConfig() + "/api/index/species_detail";
        HashMap<String, String> paramsMap = new HashMap<>();
        paramsMap.put("token", token);
        paramsMap.put("id", goodsid);
        paramsMap.put("is_material", apptype);
        RequestManager.getInstance(context).requestAsyn(url, RequestManager.TYPE_POST_FORM, paramsMap, new RequestManager.ReqCallBack() {
            @Override
            public void onReqSuccess(Object result) {
                MyLog.e("code--result", result.toString());
                Message msg = new Message();
                try {
                    JSONObject jsonObject = new JSONObject(result.toString());
                    if (jsonObject.getString("code").equals("0")) {

                        msg.obj = jsonObject.getString("msg");
                        msg.arg1 = 0;
                        handler.sendMessage(msg);
                        return;
                    }
                    JSONObject data = jsonObject.getJSONObject("data");
                    title = data.getString("title");
                    intro = data.getString("intro");
                    createtime = data.getString("createtime");
                    content = data.getString("content");


                    msg.arg1 = 2;
                    handler.sendMessage(msg);
                } catch (JSONException e) {
                    e.printStackTrace();
                    msg.arg1 = 1;
                    handler.sendMessage(msg);
                }
            }

            @Override
            public void onReqFailed(String errorMsg) {
                MyLog.e("result", errorMsg);
                if (errorMsg.equals("未登录")){
                    Message msg = new Message();
                    msg.arg1 = 11;
                    handler.sendMessage(msg);
                }
            }
        });
    }

    public Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.arg1) {
                case 0:
                    String str = (String) msg.obj;
                    Toast.makeText(context, str, Toast.LENGTH_LONG).show();
                    break;
                case 1:
                    Toast.makeText(context, textContent.getNet_error(), Toast.LENGTH_LONG).show();
                    break;
                case 2:
                    tv_title.setText(title);
                    tv_time.setText(createtime);
                    webview.getSettings().setDefaultTextEncodingName("UTF-8");
                    webview.loadDataWithBaseURL(null, content+"<style> img{max-width:100%;max-height:100%} </style>", "text/html", "utf-8", null);
                    webview.setWebViewClient(new WebViewClient());
                    break;
                case 3:
                    break;
                case 4:
                    break;
            }
        }
    };


    /**
     * 设置状态栏背景状态
     */
    public  void setTranslucentStatus()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
        {
            Window win =  getWindow();
            WindowManager.LayoutParams winParams = win.getAttributes();
            final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
            winParams.flags |= bits;
            win.setAttributes(winParams);
        }
        SystemStatusManager tintManager = new SystemStatusManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setStatusBarTintResource(0);//状态栏无背景
    }

}
